package juc.sleep;

public class SleepDemo1 {
    public static void main(String[] args)  {
        new Thread(new Thread1()).start();
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        new Thread(new Thread2()).start();
    }

    private static class Thread1 implements Runnable{
        @Override
        public void run(){
            synchronized (SleepDemo1.class) {
                System.out.println("1");
                System.out.println("2");
                try {
                    //调用wait()方法，线程会放弃对象锁，进入等待此对象的等待锁定池
                    SleepDemo1.class.wait(1000L);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("3");
                System.out.println("4");
            }
        }
    }

    private static class Thread2 implements Runnable{
        @Override
        public void run(){
            synchronized (SleepDemo1.class) {
                System.out.println("5");
                System.out.println("6");
                try {
                    //在调用sleep()方法的过程中，线程不会释放对象锁。
                    Thread.sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("7");
                System.out.println("8");
                //如果我们把代码：SleepDemo1.class.notify();给注释掉，即SleepDemo1.class调用了wait()方法，但是没有调用notify()
                //方法，则线程永远处于挂起状态。
                SleepDemo1.class.notify();
            }
        }
    }
}